<?php
/**
 * Created by PhpStorm.
 * User: chenshiqi
 * Date: 2018/9/11
 * Time: 17:28
 */

namespace app\common\model;

use think\facade\Session;
use think\Model;

/**
 * 登录模型类
 */
class Login extends Model
{
    public function makeCode($mail)
    {
        $code = rand(100000, 999999);
        $secode = [
            'verify_code' => authcode($code),
            'verify_time' => time()
        ];
        Session::set(authcode('SendEmail'.$mail), $secode);
        return $code;
    }

    public function checkCode($code,$mail){
        $key = authcode('SendEmail'.$mail);
        // 验证码不能为空
        $secode = Session::get($key);

        if(empty($secode)){
            return ['code'=>-4,'message'=>'请先获取验证码'];
        }

        if (empty($code)) {
            return ['code'=>-1,'message'=>'请输入验证码'];
        }
        // session 过期
        if (time() - $secode['verify_time'] > 1800) {
            Session::delete($key, '');
            return ['code'=>-2,'message'=>'验证码已过期'];
        }

        if (authcode(strtoupper($code)) == $secode['verify_code']) {
            Session::delete($key);
            return ['code'=>1,'message'=>'验证码校验成功'];
        }

        return ['code'=>-3,'message'=>'验证码校验失败'];
    }




    public function sendMail($to_email,$code,$type = 1)
    {

//        <tr>
//            <td colspan="3" align="center" style="background-color:#454c6d; height: 55px; padding: 30px 0"><a href="https://www.huobipro.com" target="_blank" rel="noopener"><img src="http://hbpic5.com/huobipro_static/images/20171103/logo_white.png"></a></td>
//        </tr>
//        <tr style="height: 30px;"></tr>
        switch ($type){
            case 1:
                $type_name = '您使用邮箱'.$to_email.' 正在注册CHAT ONLINE。';
                $title = '【CHAT ONLINE】账号安全中心-注册账号验证码';
                break;
            case 2:
                $type_name = '您正在CHAT ONLINE修改账户'.$to_email.'密码';
                $title = '【CHAT ONLINE】账号安全中心-重置账号密码验证码';
                break;
            default:
                $type_name = '';
                $title = '';
                break;
        }
        $html = '<table cellpadding="0" cellspacing="0" style="border: 1px solid #cdcdcd; width: 640px; margin:auto;font-size: 12px; color: #1E2731; line-height: 20px;">
        <tbody>
        

        <tr>
            <td width="20"></td>
            <td style="line-height: 40px">
                您好，<br>

                '.$type_name.'<br>

                【CHAT ONLINE】安全验证: <b>' . $code . '</b><br>

                出于安全原因，该验证码将于30分钟后失效。请勿将验证码透露给他人。<br>

                '.($type == 2?'如果您没有进行该操作，请立即修改登录密码。<br>':'').'
                </td>
            <td width="20"></td>
        </tr>
       <tr style="height: 20px;"></tr>
        <tr>
            <td width="20"></td>
            <td>
                <br>
                CHAT ONLINE<br>
                <a href="http://chat.chenshiqi.com.cn" rel="noopener" target="_blank">http://chat.chenshiqi.com.cn</a><br>
            </td>
            <td width="20"></td>
        </tr>
        <tr style="height: 20px;"></tr>
        <tr>
            <td width="20"></td>
            <td>
                <p style="color:#808080;">（这是一封自动发送的邮件，请不要直接回复）</p>
            </td>
            <td width="20"></td>
        </tr>
    <tr style="height: 50px;"></tr>
</tbody></table>';
        $re = sendEmail([$to_email], $html, $title);
        return $re;
    }

    public function sendResetSuccessMail($to_email,$name)
    {
        $html = '
尊敬的用户 '.$name.'，您好：
<p>
    &nbsp; &nbsp; &nbsp; 您在 <span style="border-bottom:1px dashed #ccc;" t="5" times=" '.date('H:i',time()).'">'.date('Y-m-d',time()).'</span> '.date('H:i:s',time()).' 修改了您在【CHAT ONLINE】的登录密码，请使用新密码登录。如有疑问，请及时联系客服人员。<br><br>
    如非本人操作，请忽略该邮件。<br><br>
    祝在CHAT ONLINE收获愉快！<br><br>
</p>
<div></div>
<div>
    <p style="color:#808080;">（这是一封自动发送的邮件，请不要直接回复）</p>
</div>
';
        sendEmail([$to_email], $html, '【CHAT ONLINE】账号安全中心-改密操作提醒');
    }
}